package com.itedge.solutionmanager.web.tags;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class ConvertMillisecondsValueTag extends BodyTagSupport
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long millis;

	/**
	 * Constructor
	 */
	public ConvertMillisecondsValueTag() {
		super();
		millis = null;
	}
	
	/**
	 * Tag name
	 */
	public String getTagName() {
		return "convertMillisValueTag";
	}

	public Long getMillis() {
		return this.millis;
	}


	public void setMillis(Long millis) {
		this.millis = millis;
	}

	/**
	 * EVAL_PAGE
	 */
	public int doEndTag() throws JspTagException {
		
		StringBuilder buff = new StringBuilder();
		long timeInSeconds = this.millis/1000;
		int seconds = (int)(timeInSeconds % 60);  
		int minutes = (int)((timeInSeconds % 3600) / 60);  
		int hours = (int)((timeInSeconds % 86400) / 3600);
		int days = (int)((timeInSeconds / 86400));
		if (days > 0) {
			buff.append(days + " d, ");
		}
		if (hours > 0) {
			buff.append(hours + " h, ");
		}
		if (minutes > 0) {
			buff.append(minutes + " min, ");
		}
		if (seconds > 0) {
			buff.append(seconds + " sec");
		}
		try {
			pageContext.getOut().print(buff.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}		
		
		return EVAL_PAGE;
	}

	/**
	 * Release this instance of tag
	 */
	public void release() {
		this.millis = null;
	}

}
